<?php

Raise::load('core.dom.RaiseDomParser');

/**
 * TestCoreRaiseDomParser class
 * Testing unit of 'phpraise/core/RaiseDomParser.php'
 *
 * @author Sam-Mauris Yong / hellclanner at live dot com
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @package Raise.UnitTest.Test
 */
class TestCoreRaiseDomParser extends RaiseTest {

    public static function testClass(){
        self::assertClassExists('RaiseDomParser');
        self::assertClassExtends('RaiseDomParser', 'RaiseObject');
        self::assertClassExists('RaiseDomNode');
        self::assertClassExtends('RaiseDomNode', 'RaiseDomElement');
    }

    public static function testParser(){
        $html = '<div class="test" id="mainDiv">Hello <b>there</b>.</div>';
        $tree = RaiseDomParser::parseString($html);
        self::assertObjectClass($tree, 'RaiseCollection');
        self::assertObjectClass($tree->get(0), 'RaiseDomNode');
        self::assertStrictEqual($tree->get(0)->tagName(), 'div');
        self::assertStrictEqual($tree->get(0)->attributes()->count(), 2);
    }

    public static function testSelectors(){
        $html = '<div class="list"><div id="header" class="subtitle hdr">This is a list</div><ul><li><a href="#top" target="_blank" title="Go > Top">First line</a></li><li class="hdr">Second line</li><li id="highlight">Third line</li><li>Fourth line</li></ul></div>';
        $tree = RaiseDomParser::parseString($html);
        self::assertObjectClass($tree, 'RaiseCollection');
        
        self::assertStrictEqual($tree->get(0)->select('div.hdr')->children()->get(0)->attributes()->toArray(), array('id' => 'header', 'class'=>'subtitle hdr'));
        self::assertStrictEqual($tree->get(0)->children()->get(0), $tree->get(0)->select('div.hdr')->children()->get(0));
        self::assertStrictEqual($tree->get(0)->select('ul li')->children()->get(1)->value(), 'Second line');
        self::assertStrictEqual($tree->get(0)->select('ul:first li:last')->value(), 'Fourth line');
        self::assertStrictEqual($tree->get(0)->select('ul:first li#highlight')->value(), 'Third line');
        self::assertStrictEqual($tree->get(0)->select('ul:first li#highlight')->children()->get(0), $tree->get(0)->select('#highlight')->children()->get(0));
    }

    public static function testGetByElementId(){
        $html = '<div class="list"><div id="header" class="subtitle hdr">This is a list</div><ul><li><a href="#top" target="_blank" title="Go > Top">First line</a></li><li class="hdr">Second line</li><li id="highlight">Third line</li><li>Fourth line</li></ul></div>';
        $tree = RaiseDomParser::parseString($html);
        
        self::assertStrictEqual($tree->get(0)->getById('highlight'), $tree->get(0)->select('#highlight'));
        self::assertStrictEqual($tree->get(0)->getById('highlight')->value(), 'Third line');
        self::assertStrictEqual($tree->get(0)->getById('highlight')->attributes()->count(), 1);
        self::assertStrictEqual($tree->get(0)->getById('header')->attributes()->count(), 2);
    }

    public static function testGetByTagName(){
        $html = '<div class="list"><div id="header" class="subtitle hdr">This is a list</div><ul><li><a href="#top" target="_blank" title="Go > Top">First line</a></li><li class="hdr">Second line</li><li id="highlight">Third line</li><li>Fourth line</li></ul></div>';
        
        $tree = RaiseDomParser::parseString($html);
        self::assertObjectClass($tree, 'RaiseCollection');
        self::assertStrictEqual($tree->get(0)->getByTagName('li')->count(), 4);
        self::assertStrictEqual($tree->get(0)->getByTagName('div')->count(), 1);
        self::assertStrictEqual($tree->get(0)->getByTagName('ul')->count(), 1);
        self::assertStrictEqual($tree->get(0)->getByTagName('ul')->get(0)->children()->count(), 4);
    }

    public static function testGetByAttrValue(){
        $html = '<div class="list"><div id="header" class="subtitle hdr">This is a list</div><ul><li><a href="#top" target="_blank" title="Go > Top">First line</a></li><li class="hdr">Second line</li><li id="highlight">Third line</li><li>Fourth line</li></ul></div>';
        $tree = RaiseDomParser::parseString($html);
        self::assertObjectClass($tree, 'RaiseCollection');
        self::assertObjectClass($tree->get(0), 'RaiseDomNode');

        self::assertStrictEqual($tree->get(0)->getByAttribute('href','#top')->count(), 1);
        self::assertStrictEqual($tree->get(0)->getByAttribute('href','#top')->get(0), $tree->get(0)->getByTagName('a')->get(0));
        self::assertStrictEqual($tree->get(0)->getByAttribute('class','hdr')->get(0), $tree->get(0)->children()->get(1)->children()->get(1));
    }


    public static function testGetByClass(){
        $html = '<div class="list"><div id="header" class="subtitle hdr">This is a list</div><ul><li><a href="#top" target="_blank" title="Go > Top">First line</a></li><li class="hdr">Second line</li><li id="highlight">Third line</li><li>Fourth line</li></ul></div>';

        $tree = RaiseDomParser::parseString($html);
        self::assertObjectClass($tree, 'RaiseCollection');
        self::assertObjectClass($tree->get(0), 'RaiseDomNode');

        self::assertStrictEqual($tree->get(0)->getByClass('hdr')->count(), 2);
        self::assertStrictEqual($tree->get(0)->getByClass('subtitle')->count(), 1);
    }
}